//
//  BottomView.m
//  duzhoumo
//
//  Created by fujin on 15/10/12.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "BottomView.h"
#import "UIScreen+Frame.h"
#import "AppColorConfig.h"
#import "AppConfig.h"
#import "UIButton+Dzm.h"
#define ButtonY 10

@interface BottomView ()
@property (copy ,nonatomic)TapBlock tapBlcok;
@end
@implementation BottomView
-(instancetype)initWithStatusArray:(NSArray *)array callBlcok:(TapBlock)tapBlcok{
   
    self = [super initWithFrame:CGRectMake(0, 0.45, [UIScreen width], 60)];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setCustomViews:array];
        self.tapBlcok = tapBlcok;
    }
    return self;
}
-(void)setCustomViews:(NSArray *)array{
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    
    if (!array || array.count == 0) {
        return;
    }
    
    CGFloat buttonHeigh = 40;
    
    //为单个的时候
    if (array.count == 1) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(15, ButtonY, [UIScreen width]- 30, buttonHeigh)];
        button.tag = 0;
        [button styleButton];
        [button setTitle:array[0] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    //多个的时候
    else{
        CGFloat buttonWidth = ([UIScreen width] - 15*(array.count+1)) / array.count;
        for (NSInteger i = 0; i < array.count ; i ++) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(15+i*(15 + buttonWidth), ButtonY, buttonWidth, buttonHeigh)];
            button.tag = i;
            
            if ((i+1) % 2 == 0) {
                [button styleButton];
            }else{
                [button style1Button];
            }
            
            [button setTitle:array[i] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button];
        }
    }
}

-(void)clickAction:(UIButton *)sender{
    self.tapBlcok(sender.tag);
}
@end
